package com.zds.support.util

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.util.Base64
import java.io.*

class SharedPreferenceUtils {
    val mEditor: SharedPreferences.Editor
    val mSharedPreferences: SharedPreferences

    constructor() {
        mSharedPreferences = AppUtils.appContext.getSharedPreferences(
                AppUtils.appContext.getPackageName(),
                Context.MODE_PRIVATE)
        mEditor = mSharedPreferences.edit()
    }

    constructor(preferencesName: String) {
        mSharedPreferences = AppUtils.appContext
                .getSharedPreferences(preferencesName, Context.MODE_PRIVATE)
        mEditor = mSharedPreferences.edit()
    }


    fun saveBoolean(key: String, value: Boolean) {
        mEditor.putBoolean(key, value).commit()
    }

    fun getBoolean(key: String, defValue: Boolean): Boolean {
        return mSharedPreferences.getBoolean(key, defValue)
    }

    fun saveInt(key: String, value: Int) {
        mEditor.putInt(key, value).commit()
    }

    fun saveDouble(key: String, value: Double) {
        mEditor.putString(key, value.toString()).commit()
    }

    fun getDouble(key: String, defValue: Double): Double {
        val value = getString(key, "")
        if (!TextUtils.isEmpty(value)) {
            try {
                return value.toDouble()
            } catch (e: Exception) {
            }

        }
        return defValue
    }

    fun getInt(key: String, defValue: Int): Int {
        return mSharedPreferences.getInt(key, defValue)
    }

    fun saveFloat(key: String, value: Float) {
        mEditor.putFloat(key, value).commit()
    }

    fun getFloat(key: String, defValue: Float): Float {
        return mSharedPreferences.getFloat(key, defValue)
    }

    fun saveString(key: String, value: String) {
        mEditor.putString(key, value).commit()
    }

    fun remove(key: String) {
        mEditor.remove(key).commit()
    }

    fun getString(key: String): String {
        return getString(key, "")
    }

    fun getString(key: String, defValue: String): String {
        val value = mSharedPreferences.getString(key, defValue)
        if (value != null) {
            return value
        }

        return ""
    }

    fun saveLong(key: String, value: Long) {
        mEditor.putLong(key, value).commit()
    }

    fun getLong(key: String, defValue: Long): Long {
        return mSharedPreferences.getLong(key, defValue)
    }

    /**
     * 保存对象，object所在的类必须实现[Serializable]接口
     *
     * @param key
     * @param object
     */
    fun saveObject(key: String, `object`: Any) {
        if (`object` is Serializable == false) {
            throw RuntimeException("请将object所在的类实现Serializable接口")
        }
        var baos: ByteArrayOutputStream? = null
        var oos: ObjectOutputStream? = null
        try {
            baos = ByteArrayOutputStream()
            oos = ObjectOutputStream(baos)
            oos.writeObject(`object`)
            val strBase64 = String(Base64.encode(baos.toByteArray(),
                    Base64.DEFAULT))
            saveString(key, strBase64)
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                oos!!.close()
                baos!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }

    fun clear() {
        mEditor.clear()
        mEditor.commit()
    }

    /**
     * 获取存储的object
     *
     * @param key
     * @param defValue
     * @return 获取成功则返回存储的object，否则返回默认值
     */
    fun getObject(key: String, defValue: Any): Any? {
        var `object`: Any? = null
        val strBase64 = getString(key, "")
        if (TextUtils.isEmpty(strBase64)) {
            return defValue
        }
        val base64 = Base64.decode(strBase64!!.toByteArray(), Base64.DEFAULT)
        var bais: ByteArrayInputStream? = null
        var bis: ObjectInputStream? = null
        try {
            bais = ByteArrayInputStream(base64)
            bis = ObjectInputStream(bais)
            `object` = bis.readObject()
            return `object`
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                bis!!.close()
                bais!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
        return defValue
    }
}